package mango.utils;

import java.util.Date;

public class SimpleLogger implements Logger, Runnable {
    private static final int TIME = 100;
    private StringBuffer log = new StringBuffer();
    private StringBuffer log2 = new StringBuffer();
    
    public SimpleLogger() {
        Thread t = new Thread(this);
        t.setName("simplelogger");
        t.setDaemon(true);
        t.start();
    }
    
    public void trace(Object msg) {
        StringBuilder sb = new StringBuilder();
        sb.append("[TRACE]").append(new Date()).append("|").append(msg).append("\n");
        log.append(sb);
    }

    public void debug(Object msg) {
        StringBuilder sb = new StringBuilder();
        sb.append("[DEBUG]").append(new Date()).append("|").append(msg).append("\n");
        log.append(sb);
    }

    public void info(Object msg) {
        StringBuilder sb = new StringBuilder();
        sb.append("[INFO]").append(new Date()).append("|").append(msg).append("\n");
        log.append(sb);
    }

    public void info(Object msg, Throwable e) {
        StringBuilder sb = new StringBuilder();
        StringBuilder trace = new StringBuilder();
        getTrace(e, trace);
        sb.append("[INFO]").append(new Date()).append("|").append(msg).append(trace).append("\n");
        log.append(sb);
    }

    public void warn(Object msg) {
        StringBuilder sb = new StringBuilder();
        sb.append("[WARN]").append(new Date()).append("|").append(msg).append("\n");
        log.append(sb);
    }

    public void error(Object msg) {
        StringBuilder sb = new StringBuilder();
        sb.append("[ERROR]").append(new Date()).append("|").append(msg).append("\n");
        log.append(sb);
    }

    public void error(Object msg, Throwable t) {
        StringBuilder sb = new StringBuilder();
        StringBuilder trace = new StringBuilder();
        getTrace(t, trace);
        sb.append("[ERROR]").append(new Date()).append("|").append(msg).append(trace).append("\n");
        log.append(sb);
    }
    
    private Object getTrace(Throwable e, StringBuilder sb) {
        sb.append(" : ").append(e.getMessage());
        StackTraceElement[] traces = e.getStackTrace();
        if(traces != null && traces.length != 0) {
            for(StackTraceElement s : traces) {
                sb.append("\n    ").append(s.toString());
            }
        }
        if(e.getCause() != null) {
            sb.append("\n");
            getTrace(e.getCause(), sb);
        }
        return sb;
    }
    
    public void run() {
        while(true) {
            try {
                if(this.log.length() != 0) {
                    StringBuffer tmp = log;
                    log = log2;
                    log2 = tmp;
                    System.out.println(log2);
                    log2.setLength(0);
                }
                Thread.sleep(TIME);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
